//
//  JOContectsTableViewController.m
//  私人通讯录
//
//  Created by song on 15/1/23.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "JOContectsTableViewController.h"
#import "JOContects.h"
#import "JOEditViewController.h"
#import "JOContectCellTableViewCell.h"

@interface JOContectsTableViewController ()<UIActionSheetDelegate>


@end

@implementation JOContectsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    UIBarButtonItem *additem = self.navigationItem.rightBarButtonItem;
    UIBarButtonItem *delitem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(delitem)];
    self.navigationItem.rightBarButtonItems =@[additem ,delitem];
}

-(void)delitem
{
    self.tableView.editing = !self.tableView.editing;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JOContectCellTableViewCell  *cell = [JOContectCellTableViewCell contectCellWithTableView:tableView];
    JOContects *contect = self.contects[indexPath.row];
    cell.contect =contect;
    
    
    return cell;
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  UITableViewCellEditingStyleInsert;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    // Return the number of rows in the section.
    return self.contects.count;
}

-(NSMutableArray *)contects
{
    if (_contects ==nil) {
        _contects =[NSMutableArray array];
    }
    return _contects;
}

-(void)addViewController:(JOAddViewController *)addvc withContects:(JOContects *)contects
{
    NSLog(@"%@",contects);
    [_contects addObject:contects];
    [self.tableView reloadData];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    id des = segue.destinationViewController;
    if([des isKindOfClass:[JOAddViewController class]])
    {
        JOAddViewController *addvc = des;
        addvc.delegate = self;
    }else if([des isKindOfClass:[JOEditViewController class]])
    {
        JOEditViewController *edvc = des;
        NSIndexPath  *indexpath =  [self.tableView indexPathForSelectedRow];
        edvc.contect = _contects[indexpath.row];
        edvc.delegate = self;
    }

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(editingStyle == UITableViewCellEditingStyleDelete)
    {
        NSLog(@"%@",indexPath);
        [self.contects removeObjectAtIndex:indexPath.row];
        
        
//        [self.tableView reloadData];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }else{
        
    }
}


-(void)EditViewControllerWithContect:(JOContects *)contect
{
//    [self.contects repla];
    
    [self.tableView reloadData];
}

- (IBAction)logout:(id)sender {
    
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"确定要取消？" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
    
    sheet.delegate = self;
    [sheet showInView:self.view];
    
    
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex == 0)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
@end
